package com.spr.service;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.spr.exception.*;
import com.spr.model.Position;
import com.spr.repository.*;

@Service
public class PositionServiceImpl implements PositionService{

	 @Resource
     private PositionRepository positionRepository;
	 
	 @Resource
	 private FacultyRepository FacultyRepository;
	 
	 @Resource
	 private ScheduleRepository ScheduleRepository;
	 
	
	
	@Override
    @Transactional
    public Position create(Position position) {
            Position createdPosition = position;
            return positionRepository.save(createdPosition);
			//return createdPosition;
    }   
	
	@Override
    @Transactional
    public Position findById(int id) {
            return positionRepository.findOne(id);
    }
	
	 @Override
     @Transactional(rollbackFor=RegisterNotFound.class)
     public Position delete(int id) throws RegisterNotFound {
             Position deletedPosition = positionRepository.findOne(id);
             
             if (deletedPosition == null)
                     throw new RegisterNotFound();
             
             positionRepository.delete(deletedPosition);
             return deletedPosition;
     }

     @Override
     @Transactional
     public List<Position> findAll() {
             return positionRepository.findAll();
     }

     @Override
     @Transactional(rollbackFor=RegisterNotFound.class)
     public Position update(Position position) throws RegisterNotFound {
             Position updatedPosition = positionRepository.findOne(position.getPositionID());

             
             if (updatedPosition == null)
                     throw new RegisterNotFound();
             
             updatedPosition.setScheduleID(position.getScheduleID());
             updatedPosition.setTitle(position.getTitle());
             updatedPosition.setDepartment(position.getDepartment());
             updatedPosition.setType(position.getType());
             updatedPosition.setHourlyRate(position.getHourlyRate());
            
             return updatedPosition;
     }
}
